package com.leo.springbootmultithreadbasic.buildthread.implementcallable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * <p><b>创建线程的方式三：实现Callable接口</b></p>
 *
 * @author Leo
 * @version V1.0
 * @date 2021/10/30 9:17 PM
 */
public class MyCallable implements Callable {

    private static Logger logger = LoggerFactory.getLogger(MyCallable.class);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask futureTask = new FutureTask(new MyCallable());
        new Thread(futureTask).start();

        logger.info("子线程返回的值：{}", futureTask.get());

    }



    @Override
    public Object call() throws Exception {

        int i = 0;
        for (; i < 5; i++) {
            logger.info("ThreadName={}, {}", Thread.currentThread().getName(), i);
        }

        return i;
    }
}
